package com.hw.algorithm.leetcode.string;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @program: algorithm
 * @description:
 * @author: hewei
 * @date: 2023-04-26 14:26
 **/
public class LeetCodeString20 {
    public static void main(String[] args) {

//        System.out.println(isValid("()[]{}"));
        System.out.println(isValid("]"));
    }

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        Map<Character,Character> map = new HashMap<>();
        final char comChar='?';
        if (s.contains(String.valueOf(comChar))){return false;}
        map.put('(',')');
        map.put('[',']');
        map.put('{','}');
        map.put(comChar,comChar);
        stack.push(comChar);
        for (int i =0;i<s.length();i++) {
            if (map.containsKey(s.charAt(i))) {
                stack.push(s.charAt(i));
            }
            if (map.values().contains(s.charAt(i))){
                if (stack.size() > 0 && map.get(stack.pop()) != s.charAt(i)){
                    return false;
                }
            }
        }
        if (stack.size() > 1){return false;}
        return true;
    }
}
